<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/28
 * Time: 17:06
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\LiveService;
use Illuminate\Http\Request;
use App\Constant\ErrorConstant;
use Illuminate\Support\Facades\Validator;

class LiveController extends BaseController
{
    protected $service;

    public function __construct(Request $request, LiveService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 直播列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listLive()
    {
        $page   =   $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->liveList((int) $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_LIVE_LIST_ERROR, $result);
        }
    }

    /**
     * 直播详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoLive()
    {
        $validator  =   Validator::make($this->request->post(), [
            'room_id'   =>  'required|integer'//|exists:mongodb.live,Id'
        ], [
            'room_id.required'  =>  '直播间号不能为空',
            'room_id.integer'   =>  '直播间号必须为整型',
            'room_id.exists'    =>  '直播间不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_LIVE_DETAIL_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');
        [$flag, $result]    =   $this->service->liveInfo($room_id);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_LIVE_DETAIL_ERROR, $result);
        }
    }

    /**
     * 直播间海报生成
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function posterLive()
    {
        $validator  =   Validator::make($this->request->post(), [
            'room_id'   =>  'required|integer'//|exists:mongodb.live,Id'
        ], [
            'room_id.required'  =>  '直播间号不能为空',
            'room_id.integer'   =>  '直播间号必须为整型',
            'room_id.exists'    =>  '直播间不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_LIVE_DETAIL_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');
        [$flag, $result]    =   $this->service->livePoster($room_id);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_LIVE_DETAIL_ERROR, $result);
        }
    }

    protected function createTest()
    {
        for ($i = 1; $i <= 5; $i++) {
            dump(implode('-', $this->singleTest()));
        }
        dd('done');
    }

    protected function singleTest()
    {
        $total_ball =   range(1, 33);
        shuffle($total_ball);

        $red_ball   =   [];
        for ($i = 1; $i <= 6; $i++) {
            $single =   array_rand($total_ball);
            $red_ball[] =   $total_ball[$single];
            unset($total_ball[$single]);
            shuffle($total_ball);
            shuffle($total_ball);
            shuffle($total_ball);
            shuffle($total_ball);
            shuffle($total_ball);
            shuffle($total_ball);
            shuffle($total_ball);
        }

        sort($red_ball);
        $green_ball =   [rand(1, 16)];
        return array_merge($red_ball, $green_ball);
    }

}
